from scipy.misc import imread
from scipy.misc import imsave
from scipy.signal import convolve2d
import matplotlib.pyplot as plt
import logging
import numpy as np
from dipmod.histogram import show_gray_image


def main():
    FORMAT = '	[%(levelname)-5s]%(asctime)-8s %(filename)s:%(lineno)d %(message)s'
    DATEFORMAT = '%H:%M:%S'
    logging.basicConfig(level=logging.DEBUG, format=FORMAT, datefmt=DATEFORMAT)
    laplacianWindow = np.matrix([
        [0, 1, 0],
        [1, -4, 1],
        [0, 1, 0]
    ])

    sobelWindowhx = np.matrix(
        [[-1, 0, 1],
         [-2, 0, 2],
         [-1, 0, 1]]
    )
    sobelWindowhy = np.matrix(
        [[1, 2, 1],
         [0, 0, 0],
         [-1, -2, -1]]
    )
    aveWindow = np.ones((5, 5)) / 25.0

    oriimg = imread("/home/d/Documents/lu-ht/DIP/images/skeleton_orig.tif")
    oriimg = oriimg.astype(float) / 255.0

    imgb = convolve2d(oriimg, laplacianWindow, 'same', 'symm')
    imgc = oriimg + imgb
    sobelx = convolve2d(oriimg, sobelWindowhx, 'same', 'symm')
    sobely = convolve2d(oriimg, sobelWindowhy, 'same', 'symm')
    imgd = np.sqrt(sobelx * sobelx + sobely * sobely)
    imge = convolve2d(imgd, aveWindow, 'same', 'symm')
    imgf = imgc * imge
    imgg = oriimg + imgf
    imgh = np.power(imgg, 0.6)
    logging.debug(oriimg.shape)
    logging.debug(laplacianWindow)
    plotlayout = 330
    plt.subplot(plotlayout + 1)
    plt.imshow(oriimg, cmap='Greys_r')
    plt.subplot(plotlayout + 2)
    plt.imshow(imgb, cmap='Greys_r')
    plt.subplot(plotlayout + 3)
    plt.imshow(imgc, cmap='Greys_r')
    plt.subplot(plotlayout + 4)
    plt.imshow(imgd, cmap='Greys_r')
    plt.subplot(plotlayout + 5)
    plt.imshow(imge, cmap='Greys_r')
    plt.subplot(plotlayout + 6)
    plt.imshow(imgf, cmap='Greys_r')
    plt.subplot(plotlayout + 7)
    plt.imshow(imgg, cmap='Greys_r')
    plt.subplot(plotlayout + 8)
    plt.imshow(imgh, cmap='Greys_r')
    plt.show()

    # imgb = convolve2d()
    pass


if __name__ == '__main__':
    main()
